package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.code.CodeClass;
import com.example.demo.code.CodeScore;
import com.example.demo.dao.DaoUserTest;
import com.example.demo.pojo.*;
import com.example.demo.server.ClassServer;
import com.example.demo.server.QuestionSever;
import com.example.demo.server.VideoSever;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Controller
public class ClassController {
    @Autowired
    ClassServer classServer;
    @Autowired
    VideoSever videoSever;
    @Autowired
    QuestionSever questionSever;

    @CrossOrigin
    @PostMapping(value = "api/getClassList")
    @ResponseBody
    List<Classed> findAllClass(@RequestBody JSONObject jsonObject) {
        int index = jsonObject.getIntValue("index") - 1;
        String key = jsonObject.getString("key");
        String userId = jsonObject.getString("userId");
        return classServer.findLikeNameByIndex(index, key, userId);
    }

    @CrossOrigin
    @PostMapping(value = "api/getClassNum")
    @ResponseBody
    Long getClassNum(@RequestBody JSONObject jsonObject) {
        String key = jsonObject.getString("key");
        String userId = jsonObject.getString("userId");
        return classServer.getClassedNum(key, userId);
    }

    @CrossOrigin
    @PostMapping(value = "api/addClassUser")
    @ResponseBody
    int getClassNum(@RequestBody ClassUser classUser) {
        return classServer.saveClassUser(classUser);
    }

    @CrossOrigin
    @PostMapping(value = "api/uploadClassImg")
    @ResponseBody
    public String addClassImg(@RequestParam("file") MultipartFile file) {
        return classServer.addClassImg(file);
    }

    @CrossOrigin
    @PostMapping(value = "api/uploadClass")
    @ResponseBody
    public CodeClass addClass(@RequestBody Classed classed) {
        return classServer.addClassed(classed);
    }

    @CrossOrigin
    @PostMapping(value = "api/deleteClass")
    @ResponseBody
    public CodeClass deleteClass(@RequestBody JSONObject jsonObject) {
        String s = jsonObject.getString("id");
        return classServer.deleteClass(s);
    }

    @CrossOrigin
    @PostMapping(value = "api/uploadVideo")
    @ResponseBody
    public int addClassVideo(@RequestParam("file") MultipartFile file, @RequestParam("name") String name, @RequestParam("classId") String classId) {
        return videoSever.saveVideoPath(file, name, classId);
    }

    @CrossOrigin
    @PostMapping(value = "api/getVideoList")
    @ResponseBody
    public List<Video> getVideoList(@RequestBody JSONObject jsonObject) {
        String classId = jsonObject.getString("classId");
        return videoSever.getAllByClassId(classId);
    }

    @CrossOrigin
    @GetMapping(value = "api/getClassList")
    @ResponseBody
    public List<Map<String, String>> getClassInfoList() {
        return classServer.getAllClass();
    }

    @CrossOrigin
    @PostMapping(value = "api/uploadTest")
    @ResponseBody
    public String uploadTest(@RequestBody String params) {
        JSONObject j1 = JSONObject.parseObject(params);
        List<Question> nodes = JSONObject.parseArray(j1.getJSONArray("data").toJSONString(), Question.class);
        return questionSever.write2xml(nodes);

    }

    @CrossOrigin
    @PostMapping(value = "api/uploadTestMessage")
    @ResponseBody
    public void uploadTestMessage(@RequestBody TestPaper testPaper) {
        questionSever.saveTestPaper(testPaper);
    }

    @CrossOrigin
    @PostMapping(value = "api/getTestMessage")
    @ResponseBody
    public List<TestPaper> getAllTestByKey(@RequestBody JSONObject jsonObject) {
        int index = jsonObject.getIntValue("index") - 1;
        String key = jsonObject.getString("key");
        return questionSever.getTestPaperByKey(key, index);

    }

    @CrossOrigin
    @PostMapping(value = "api/countTestMessage")
    @ResponseBody
    public int countTestByKey(@RequestBody JSONObject jsonObject) {
        String key = jsonObject.getString("key");
        return questionSever.countTestPaperByKey(key);

    }

    @CrossOrigin
    @PostMapping(value = "api/getTestPaper")
    @ResponseBody
    public List<Question> getTestPaper(@RequestBody JSONObject jsonObject) {
        String name = jsonObject.getString("name");
        int type=jsonObject.getIntValue("type");
        return questionSever.xml2List(name,type);

    }
    @CrossOrigin
    @PostMapping(value = "api/putStu")
    @ResponseBody
    public void putStu(@RequestBody JSONObject jsonObject) {
        String name = jsonObject.getString("testId");
        questionSever.putStu(name);

    }
    @CrossOrigin
    @PostMapping(value = "api/getUserTest")
    @ResponseBody
    public List<TestPaper> getUserTest(@RequestBody JSONObject jsonObject) {
        String id=jsonObject.getString("userId");
        String key=jsonObject.getString("key");
        int index=jsonObject.getIntValue("index");
        return questionSever.getTestPaperByKeyAndUser(id,key,index);
    }
    @CrossOrigin
    @PostMapping(value = "api/delTest")
    @ResponseBody
    int delUser(@RequestBody JSONObject jsonObject) {
        String id=jsonObject.getString("id");
        return  questionSever.removeById(id);

    }
    @CrossOrigin
    @PostMapping(value = "api/countUserTest")
    @ResponseBody
    long countUserTest(@RequestBody JSONObject jsonObject) {
        String id=jsonObject.getString("userId");
        String name=jsonObject.getString("key");
        return  questionSever.countByName(name,id);
    }
    @CrossOrigin
    @PostMapping(value = "api/getScore")
    @ResponseBody
    CodeScore getScore (@RequestBody JSONObject jsonObject) {
        String userId=jsonObject.getString("userId");
        String testId=jsonObject.getString("testId");
        return  questionSever.getCodeScore(testId,userId);
    }
    @CrossOrigin
    @PostMapping(value = "api/getTestById")
    @ResponseBody
    TestPaper getTestById(@RequestBody JSONObject jsonObject) {
        String id=jsonObject.getString("id");
        return  questionSever.getTestById(id);

    }
    @CrossOrigin
    @PostMapping(value = "api/uploadAnswer")
    @ResponseBody
    public String uploadAnswer(@RequestBody String params) {
        JSONObject j1 = JSONObject.parseObject(params);
        List<String> nodes = JSONObject.parseArray(j1.getJSONArray("data").toJSONString(), String.class);
        return questionSever.answer2xml(nodes);
    }
    @CrossOrigin
    @PostMapping(value = "api/uploadTestUser")
    @ResponseBody
    public void uploadAnswer(@RequestBody TestUser testUser) {
        questionSever.uploadTestUser(testUser);
    }
    @CrossOrigin
    @PostMapping(value = "api/getUserAnswer")
    @ResponseBody
    public List<String > getUserAnswer(@RequestBody JSONObject jsonObject) {
        String answer=jsonObject.getString("file");
        return questionSever.getUserAnswer(answer);
    }

    @CrossOrigin
    @GetMapping(value = "api/getAllTest")
    @ResponseBody
    public List<TestPaper> getAllTest()  {

        return questionSever.getAllTest();
    }
    @CrossOrigin
    @PostMapping(value = "api/getA1Data")
    @ResponseBody
    public List<StringXY> getA1Data(@RequestBody JSONObject jsonObject)  {
            String classId=jsonObject.getString("classId");
            return questionSever.getAllXYBy5(classId);
    }
    @CrossOrigin
    @PostMapping(value = "api/getB1Data")
    @ResponseBody
    public List<StringXY> getB1Data(@RequestBody JSONObject jsonObject)  {
        String classId=jsonObject.getString("classId");
        return questionSever.getAllXYInfo(classId);
    }
    @CrossOrigin
    @PostMapping(value = "api/getB2Data")
    @ResponseBody
    public List<StringXY> getB2Data(@RequestBody JSONObject jsonObject)  {
        String classId=jsonObject.getString("classId");
        int stride=jsonObject.getIntValue("stride");
        return questionSever.getXYByStride(classId,stride);
    }

}
